package noema.humanTask;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import noema.dataModel.MyTask;

import org.drools.runtime.process.WorkItem;
import org.drools.runtime.process.WorkItemHandler;
import org.drools.runtime.process.WorkItemManager;

import flexjson.JSONSerializer;

@Stateless
public class MyHumanTaskHandler implements WorkItemHandler {

	@PersistenceContext(unitName = "application.data")
	EntityManager em;

	@Override
	public void abortWorkItem(WorkItem i, WorkItemManager m) {
		// Do nothing
	}

	@Override
	public void executeWorkItem(WorkItem i, WorkItemManager m) {
		JSONSerializer serializer = new JSONSerializer();
		String jsonParams = serializer.deepSerialize(i.getParameters());
		String owner = (String) i.getParameter("ActorId");
		if (owner == null) {
			owner = "all";
		}
		MyTask task = new MyTask(i.getId(), i.getProcessInstanceId(),
				i.getName(), jsonParams, owner);
		em.persist(task);
	}

}
